{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <center>Logs Analysis (UNSW-NB15)</center>\n",
    "Reads models logs and plot discriminator & generator loss, discriminator on generated & real data samples. The plots are saved as .png file in folder imgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle, os\n",
    "import pandas as pd\n",
    "from collections import defaultdict\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "sns.set_style(\"darkgrid\")\n",
    "# %matplotlib inline\n",
    "\n",
    "( _, _, filenames) = next(os.walk('logs'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_summary(d_l, g_l,acc_r,acc_g, m =''):\n",
    "    n = np.arange(len(d_l))\n",
    "    title = 'Loss and Accuracy plot'+'\\n'+ m\n",
    "    title = title.replace('.pickle','')\n",
    "    fig, axs = plt.subplots(2,figsize=(19.20,10.80))\n",
    "\n",
    "    axs[0].set_title(title,fontsize=20.0,fontweight=\"bold\")\n",
    "    axs[0].plot(n, g_l,label='Generator loss',linewidth=4)\n",
    "    axs[0].plot(n, d_l,label='Discriminator loss',linewidth=4)\n",
    "    axs[0].legend(loc=0, prop={'size': 20})\n",
    "    axs[0].set_ylabel('Loss',fontsize=20.0,fontweight=\"bold\")\n",
    "    axs[0].tick_params(labelsize=20)\n",
    "    axs[0].tick_params(axis='x',which='both',bottom=False,top=False,labelbottom=False,labelsize=20)\n",
    "\n",
    "    # axs[1].plot(n, acc,'r',label='Discriminator accuracy',linewidth=4)\n",
    "    axs[1].plot(n, acc_g,label='Accuracy on Generated',linewidth=4)\n",
    "    axs[1].plot(n, acc_r,label='Accuracy on Real',linewidth=4)\n",
    "    axs[1].legend(loc=0,prop={'size': 20})\n",
    "    axs[1].set_ylabel('Accuracy',fontsize=20.0,fontweight=\"bold\")\n",
    "    axs[1].set_xlabel('Ephoc',fontsize=20.0,fontweight=\"bold\")\n",
    "    axs[1].tick_params(labelsize=20)\n",
    "\n",
    "    plt.tight_layout()\n",
    "    #plt.show()\n",
    "    if not os.path.exists(\"imgs\"):\n",
    "        os.makedirs('imgs')\n",
    "    plt.savefig(f'imgs/{m[:-7]}.png',dpi = 300)\n",
    "    plt.close('all') #plt.close(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for filename in filenames :\n",
    "    with open('logs/'+filename, 'rb') as f:\n",
    "        x = pickle.load(f)\n",
    "\n",
    "    d_l = np.array(x['discriminator_loss']).ravel()\n",
    "    g_l = np.array(x['Generator_loss']).ravel()\n",
    "    acc_history = np.array(x['acc_history'])\n",
    "    acc = acc_history.sum(axis=1) * 0.5\n",
    "    acc_real = acc_history[:,1]\n",
    "    acc_gen = acc_history[:,0]\n",
    "\n",
    "    plot_summary(d_l, g_l,acc_real,acc_gen,filename)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
